'use strict';

const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE } = app.Sequelize;

  const Course = app.model.define('course', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    name: { type: STRING(100), allowNull: false, comment: '课程名称' },
    description: { type: TEXT, comment: '课程简介' },
    logo: { type: STRING(255), comment: '课程图片' },
    image: { type: TEXT, comment: '课程图片' },
    location: { type: STRING(255), allowNull: false, comment: '上课地点' },
    schedule: { type: STRING(255), allowNull: false, comment: '上课时间' },
    grade: { type: STRING(255), allowNull: false, comment: '上课年级' },
    teacher_name: { type: STRING(50), allowNull: false, comment: '教师姓名' },
    teacher_info: { type: TEXT, comment: '教师简介' },
    status: { type: INTEGER, defaultValue: 1, comment: '状态：1-正常，0-禁用' },
    total_count: { type: INTEGER, defaultValue: 0, comment: '总人数' },
    signed_count: { type: INTEGER, defaultValue: 0, comment: '已报名人数' },
    create_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    }
  }, {
    tableName: 'course',
    timestamps: true,
    updatedAt: 'updated_time',
    createdAt: 'create_time',
  });

  Course.associate = function() {
    // 关联报名记录
    app.model.Course.hasMany(app.model.CourseRecord, {
      foreignKey: 'course_id',
      as: 'courseRecordList'
    });
  };

  return Course;
};
